
#ifndef CODING_INTERVIEWS_LISTNODE_H
#define CODING_INTERVIEWS_LISTNODE_H

#include <vector>

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}


    friend std::ostream &operator<<(std::ostream &out, ListNode *node) {
        out << "ListNode(";
        while (node != nullptr) {
            out << node->val;
            node = node->next;
            if (node != nullptr) {
                out << ", ";
            }
        }
        out << ")";
        return out;
    }
};

ListNode *buildList(const std::vector<int> &list) {
    ListNode *head = new ListNode(0);
    ListNode *p = head;
    for (int num: list) {
        p->next = new ListNode(num);
        p = p->next;
    }
    p = head;
    head = head->next;
    delete p;
    return head;
}

void freeList(ListNode *node) {
    while (node != nullptr) {
        ListNode *p = node;
        node = node->next;
        delete p;
    }
}

#endif